#include "Hub.hpp"

Hub::Hub(App* appPtr,int* life,int* Banana) : m_app(appPtr), m_Ballon_int(life), m_Banana_int(Banana)
{

}

void Hub::Loading()
{
    // Loading of Sprite
    for(int i = 0;i < 10;i++)
    {
        m_Number_I[i] = m_app->m_ressourceManager.AddImage("Number_" + i, "images/hub/" + FileParser::ToString(i) + ".png");
        m_Number_S[i].SetImage(*(m_Number_I[i]->Ressource()));
    }

    m_Banana.AddFrame( new Frame(m_app->m_ressourceManager.AddImage("Animation_Banana", "images/Hub/banana.png"), sf::IntRect(0, 0, 12, 16)));
    m_Banana.AddFrame( new Frame(m_app->m_ressourceManager.Image("Animation_Banana"), sf::IntRect(13, 0, 25, 16)));
    m_Banana.AddFrame( new Frame(m_app->m_ressourceManager.Image("Animation_Banana"), sf::IntRect(26, 0, 38, 16)));
    m_Banana.AddFrame( new Frame(m_app->m_ressourceManager.Image("Animation_Banana"), sf::IntRect(39, 0, 51, 16)));
    m_Banana.AddFrame( new Frame(m_app->m_ressourceManager.Image("Animation_Banana"), sf::IntRect(52, 0, 64, 16)));
    m_Banana.AddFrame( new Frame(m_app->m_ressourceManager.Image("Animation_Banana"), sf::IntRect(65, 1, 77, 16)));
    m_Banana.AddFrame( new Frame(m_app->m_ressourceManager.Image("Animation_Banana"), sf::IntRect(78, 1, 90, 16)));
    m_Banana.AddFrame( new Frame(m_app->m_ressourceManager.Image("Animation_Banana"), sf::IntRect(91, 1, 103, 16)));

    m_Ballon.AddFrame( new Frame(m_app->m_ressourceManager.AddImage("Animation_Ballon", "images/Hub/Ballon.png"), sf::IntRect(72, 0, 85, 21)));
    m_Ballon.AddFrame( new Frame(m_app->m_ressourceManager.AddImage("Animation_Ballon"), sf::IntRect(90, 0, 103, 21)));
    m_Ballon.AddFrame( new Frame(m_app->m_ressourceManager.AddImage("Animation_Ballon"), sf::IntRect(108, 0, 121, 21)));
    m_Ballon.AddFrame( new Frame(m_app->m_ressourceManager.AddImage("Animation_Ballon"), sf::IntRect(127, 0, 140, 21)));
    m_Ballon.AddFrame( new Frame(m_app->m_ressourceManager.AddImage("Animation_Ballon"), sf::IntRect(108, 0, 121, 21)));
    m_Ballon.AddFrame( new Frame(m_app->m_ressourceManager.AddImage("Animation_Ballon"), sf::IntRect(90, 0, 103, 21)));
    m_Ballon.AddFrame( new Frame(m_app->m_ressourceManager.AddImage("Animation_Ballon"), sf::IntRect(72, 0, 85, 21)));
    m_Ballon.AddFrame( new Frame(m_app->m_ressourceManager.AddImage("Animation_Ballon"), sf::IntRect(54, 0, 67, 21)));
    m_Ballon.AddFrame( new Frame(m_app->m_ressourceManager.AddImage("Animation_Ballon"), sf::IntRect(36, 0, 49, 21)));
    m_Ballon.AddFrame( new Frame(m_app->m_ressourceManager.AddImage("Animation_Ballon"), sf::IntRect(18, 0, 31, 21)));
    m_Ballon.AddFrame( new Frame(m_app->m_ressourceManager.AddImage("Animation_Ballon"), sf::IntRect(0, 0, 13, 21)));
    m_Ballon.AddFrame( new Frame(m_app->m_ressourceManager.AddImage("Animation_Ballon"), sf::IntRect(18, 0, 31, 21)));
    m_Ballon.AddFrame( new Frame(m_app->m_ressourceManager.AddImage("Animation_Ballon"), sf::IntRect(36, 0, 49, 21)));
    m_Ballon.AddFrame( new Frame(m_app->m_ressourceManager.AddImage("Animation_Ballon"), sf::IntRect(54, 0, 67, 21)));

    m_Banana.SetFrameTime(0.1);
    m_Banana.SetPosition(20,20);
    m_Banana.Play();

    m_Ballon.SetPosition((m_app->m_window.GetWidth() - 40),20);
    m_Ballon.SetFrameTime(0.1);
    m_Ballon.Play();

    m_Display_Life = true;
    m_Display_banana = true;
    m_DisplayTime.Reset();

    *m_Ballon_int = 18;
    *m_Banana_int = 52;

    m_DisplayTime.Reset();

    m_LastDisplayTime = 0.0;
}

void Hub::SetTitle(std::string TitleMap)
{
    m_Title.SetText(TitleMap);
    m_Title.SetPosition((m_app->m_window.GetWidth()/2 - m_Title.GetRect().GetWidth()/2),(m_app->m_window.GetHeight() - 50));
}

void Hub::SetDisplay(DisplayType Type)
{
    switch(Type)
    {
        case Life:
            m_Display_Life = true;
        break;
        case Banana:
            m_Display_banana = true;
        break;
    }
}

void Hub::UpdateHub(float ElapsedTime)
{
    m_Ballon.UpdateAnim(ElapsedTime);
    m_Banana.UpdateAnim(ElapsedTime);
}

sf::Sprite Hub::DrawHub(int NumberOfTime)
{
    if((m_DisplayTime.GetElapsedTime() - m_LastDisplayTime) <= 5.0)
    {
        switch(NumberOfTime)
        {
            case 0:
                return m_Ballon;
            break;
            case 1:
                return m_Banana;
            break;
            case 2:
                if(m_Display_Life)
                {
                    if(*m_Ballon_int < 10)
                    {
                        m_Number_S[*m_Banana_int].SetPosition((m_app->m_window.GetWidth() - 52),22);
                        return m_Number_S[*m_Banana_int];
                    }
                    else
                    {
                        m_Number_S[*m_Banana_int % 10].SetPosition((m_app->m_window.GetWidth() - 52),22);
                        return m_Number_S[*m_Banana_int % 10];
                    }
                }
                else
                    return sf::Sprite();
            break;
            case 3:
                if(m_Display_Life)
                {
                    if(*m_Ballon_int > 9)
                    {
                        m_Number_S[int(floor(*m_Ballon_int / 10))].SetPosition((m_app->m_window.GetWidth() - 60),22);
                        return m_Number_S[int(floor(*m_Ballon_int / 10))];
                    }
                    else
                    {
                        return sf::Sprite();
                    }
                }
                else
                    return sf::Sprite();
            break;
            case 4:
                if(m_Display_banana)
                {
                    if(*m_Banana_int < 10)
                    {
                        m_Number_S[*m_Banana_int].SetPosition(40,22);
                        return m_Number_S[*m_Banana_int];
                    }
                    else
                    {
                        m_Number_S[*m_Banana_int % 10].SetPosition(50,22);
                        return m_Number_S[*m_Banana_int % 10];
                    }
                }
                else
                    return sf::Sprite();
            break;
            case 5:
                if(m_Display_banana)
                {
                    if(*m_Banana_int > 9)
                    {
                        m_Number_S[int(floor(*m_Banana_int / 10))].SetPosition(40,22);
                        return m_Number_S[int(floor(*m_Banana_int / 10))];
                    }
                    else
                    {
                        return sf::Sprite();
                    }
                }
                else
                    return sf::Sprite();
            break;
            default:
                return sf::Sprite();
            break;
        }
    }
    else
    {
        m_Display_banana = false;
        m_Display_Life = false;
        return sf::Sprite();
    }
}

sf::String Hub::DrawTitle()
{
    if(m_DisplayTime.GetElapsedTime() < 5.0)
        return m_Title;
    else
        return sf::String();
}
